﻿using DragonSolution.Web.Data.Entity;
using DragonSolution.Web.Data.Enum;
using DragonSolution.Web.UI.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DragonSolution.Web.UI.Areas.AdminMrg.Controllers
{
    [CheckLogin(true)]
    public class MProductController : AdminBaseController
    {
        private int type = (int)Subject.产品;
        private MyDbContext db;
        public MProductController()
        {
            db = base.GetContext();
        }

        // GET: AdminMrg/MProduct
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 分类管理
        /// </summary>
        /// <returns></returns>
        public ActionResult TypeList()
        {
            return View();
        }

        public ActionResult SelectType(int id)
        {
            ViewBag.cParentId = id;
            ViewBag.rootChkDisabled = true;
            return View();
        }

        /// <summary>
        /// 添加/编辑分类
        /// </summary>
        /// <returns></returns>
        public ActionResult AddType(int id)
        {
            //id=0表示添加
            if (id != 0)
            {
                Category c = db.Category.Find(id);
                if (c != null)
                {
                    ViewBag.cId = c.CategoryID;
                    ViewBag.cName = c.CategoryName;
                    ViewBag.cParentId = c.ParentID;
                }
            }
            return View();
        }

        public ActionResult AddProduct()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddProduct(string ProductName, bool isTop, string ProductPostTime, int ProductCategory,string FileIDList, string ProductContent)
        {
            try
            {
                DateTime time = string.IsNullOrEmpty(ProductPostTime) ? DateTime.Now : Convert.ToDateTime(ProductPostTime);
                int is_top = isTop ? 1 : 0;
                ProductInfo pro = new ProductInfo() { ProductCategory = ProductCategory, ProductName = ProductName, ProductContent = ProductContent, ProductPostTime = time, IsDelete = 0, IsTop = is_top, ViewNum = 0 };
                db.ProductInfo.Add(pro);
                db.SaveChanges();
                int newID = pro.ProductID;

                //修改附件表的"所属表ID"字段
                if (!string.IsNullOrEmpty(FileIDList))
                {
                    var attrIds = FileIDList.Trim(',').Split(',');
                    Attachment attr = null;
                    foreach (var id in attrIds)
                    {
                        attr = db.Attachment.Find(Convert.ToInt32(id));
                        if (attr != null)
                        {
                            attr.TableID = newID;
                            db.SaveChanges();
                        }
                    }
                }

                ViewBag.msg = "添加成功";
            }
            catch (Exception)
            {
                ViewBag.msg = "添加失败";
            }
            return View();
        }

        public ActionResult EditProduct(int id) {
            try
            {
                ProductInfo pro = db.ProductInfo.Find(id);
                if (pro != null)
                {
                    int typeId = pro.ProductCategory;
                    //类型名
                    Category c = db.Category.Find(typeId);
                    if (c != null)
                    {
                        ViewBag.typeName = c.CategoryName;
                    }

                    return View(pro);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch (Exception)
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult EditProduct(ProductInfo product,string FileIDList)
        {
            int pId = product.ProductID;
            ProductInfo p = db.ProductInfo.Find(pId);//找到要更新的产品
            try
            {
                p.ProductName = product.ProductName;
                p.IsTop = product.IsTop;
                p.ProductCategory = product.ProductCategory;
                p.ProductContent = product.ProductContent;
                p.ProductPostTime = product.ProductPostTime;
                db.SaveChanges();
                int typeId = p.ProductCategory;
                Category c = db.Category.Find(typeId);
                if (c != null)
                {
                    ViewBag.typeName = c.CategoryName;
                }

                //修改图片
                //先逻辑删除此产品下的所有图片
                var imgList = db.Attachment.Where(x => x.TableID == pId && x.TableName == "Attachment").ToList();
                if (imgList.Count > 0) {
                    foreach (var item in imgList)
                    {
                        item.IsDelete = 1;
                    }
                    db.SaveChanges();
                }
                //修改附件表的"所属表ID"字段，以及IsDelete = 0
                if (!string.IsNullOrEmpty(FileIDList))
                {
                    var attrIds = FileIDList.Trim(',').Split(',');
                    Attachment attr = null;
                    foreach (var id in attrIds)
                    {
                        attr = db.Attachment.Find(Convert.ToInt32(id));
                        if (attr != null)
                        {
                            attr.IsDelete = 0;
                            attr.TableID = pId;
                            db.SaveChanges();
                        }
                    }
                }

                ViewBag.msg = "更新成功";
            }
            catch (Exception)
            {
                ViewBag.msg = "更新失败";
            }
            return View();
        }
    }
}